package com.rz.paas.test.inChannel

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.LinearLayout
import com.rz.paas.test.R
import com.rz.paas.test.inChannel.adapter.VideoData
import com.rz.paas.test.inChannel.adapter.VideoListAdapter

class VideoMainView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    FrameLayout(context, attributeSet, defStyleAttr) {


    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return true
    }
    var oldContainer: ViewGroup? = null
    var oldPosition: Int = 0
    fun onActive(playerView: View, videoData: VideoData, position: Int) {

        val parent = playerView.parent
        if (parent != this ) {
            Log.d(TAG, "itemPosition: ${videoData.userId}_${videoData.deviceName} oldPosition:${oldPosition} position:${position} oldContainer:${oldContainer}")
            Log.d(TAG, "itemPosition: parent:${parent}")
            oldContainer = parent as ViewGroup?
            oldPosition = position
            (parent as ViewGroup).removeView(playerView)
        }

        val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        playerView.layoutParams = layoutParams
        addView(playerView)


    }

    var notify: Notify? = null
    fun setNotifyItem(no: Notify) {
        notify = no
    }

    private val TAG = "VideoMainView"
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        Log.d(TAG, "onDetachedFromWindow: ")
        notify = null
    }

    fun hideSelf() {
        val childAt = getChildAt(0)
        if (childAt is ImageView) {
            Log.d(TAG, "itemPosition:$this")
            removeView(childAt)
            Log.d(TAG, "itemPosition:$oldContainer")
            Log.d(TAG, "itemPosition:----------------------")
            oldContainer?.addView(childAt)
        }
        notify?.notifyChange(oldPosition)

    }
}